package cn.hftoday.org;

import java.util.*;
import org.apache.log4j.*;
import cn.hftoday.util.*;
import cn.hftoday.core.*;

/**
 * super class of all Party
 * @author lane.cn@gmail.com
 *
 */
abstract class PartyBase implements Party {
	private static final Logger LOG = Logger.getLogger(Person.class);
	
	protected String id;
	protected String name;
	protected String icon;
	protected boolean hidden;
	protected Calendar createTime = CalendarParser.INVALID_TIME;
	protected Calendar updateTime = CalendarParser.INVALID_TIME;

	/**
	 * id
	 * @return
	 */
	public String getId() {
		return id;
	}
	
	/**
	 * id
	 * @param id
	 */
	public void setId(String id) {
		this.id = id;
	}
	
	/**
	 * name
	 * @return
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * name
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * icon
	 * @return
	 */
	public String getIcon() {
		return icon;
	}
	
	/**
	 * icon
	 * @param icon
	 */
	public void setIcon(String icon) {
		this.icon = icon;
	}
	
	/**
	 * parent
	 * @return
	 */
	public Party getParent() {
		return (Party)Mapper.getInstance().queryForObject("SelectPartyByChild", getId());
	}
	
	/**
	 * parent
	 * @param parent
	 */
	public void setParent(Party parent) {
		String time = CalendarParser.toString(Calendar.getInstance());
		//String range = "";
		Mapper.getInstance().insert("InsertPartyRelation", parent.getId(), getId(), "", time, time);
	}
	
	/**
	 * children
	 * @return
	 */
	public List<Party> getChildren() {
		return (List<Party>)Mapper.getInstance().queryForList("SelectPartyByParent", getId());
	}
	
	public List<Party> getAllChildren() {
		List<Party> list = new ArrayList<Party>();
		List<Party> children = getChildren();
		for (int i = 0; i < children.size(); i ++) {
			putAllChildren(list, children.get(i));
		}
		return list;
	}
	
	private void putAllChildren(List<Party> list, Party party) {
		list.add(party);
		List<Party> children = party.getChildren();
		for (int i = 0; i < children.size(); i ++) {
			putAllChildren(list, children.get(i));
		}
	}
	
	/**
	 * children
	 * @param range
	 * @return
	 */
	public List<Party> getChildren(Party range) {
		return (List<Party>)Mapper.getInstance().queryForList("SelectPartyByParentAndRange", getId(), range.getId());
	}
	
	/**
	 * children count
	 * @return
	 */
	public int getChildrenCount() {
		return getChildren().size();
	}
	
	public int getAllChildrenCount() {
		return getAllChildren().size();
	}
	
	/**
	 * children count
	 * @param range
	 * @return
	 */
	public int getChildrenCount(Party range) {
		return getChildren(range).size();
	}
	
	/**
	 * child
	 * @param id
	 * @return
	 */
	public Party getChild(String id) {
		return (Party)Mapper.getInstance().queryForObject("SelectPartyByParentAndId", getId(), id);
	}
	
	/**
	 * add a child
	 * @param child
	 */
	public void addChild(Party child) {
		String time = CalendarParser.toString(Calendar.getInstance());
		Mapper.getInstance().insert("InsertPartyRelation", getId(), child.getId(), "", time, time);
	}
	
	/**
	 * add a child
	 * @param child
	 * @param range
	 */
	public void addChild(Party child, Party range) {
		String time = CalendarParser.toString(Calendar.getInstance());
		Mapper.getInstance().insert("InsertPartyRelation", getId(), child.getId(), range.getId(), time, time);
	}
	
	/**
	 * remove a child
	 * @param child
	 */
	public void removeChild(Party child) {
		Mapper.getInstance().delete("DeletePartyRelation", getId(), child.getId(), "");
	}

	/**
	 * remove a child
	 * @param child
	 * @param range
	 */
	public void removeChild(Party child, Party range) {
		Mapper.getInstance().delete("DeletePartyRelation", getId(), child.getId(), range.getId());
	}

	/**
	 * hidden party
	 * @return
	 */
	public boolean isHidden() {
		return hidden;
	}
	
	/**
	 * hidden party
	 * @param hidden
	 */
	public void setHidden(boolean hidden) {
		this.hidden = hidden;
	}

	public Calendar getCreateTime() {
		return createTime;
	}
	
	public void setCreateTime(Calendar createTime) {
		this.createTime = createTime;
	}
	
	public Calendar getUpdateTime() {
		return updateTime;
	}
	
	public void setUpdateTime(Calendar updateTime) {
		this.updateTime = updateTime;
	}
}
